#include "InputEventHandler.h"
#include "IKeyListener.h"

namespace steam
{
	namespace core
	{

		void InputEventHandler::addKeyListener( intake::IKeyListener* keyListener )
		{
			m_keyListeners.push_back( keyListener );
		}

		void InputEventHandler::removeKeyListener( intake::IKeyListener* keyListener )
		{
			std::vector<intake::IKeyListener*>::iterator it = m_keyListeners.end();
			for (std::vector<intake::IKeyListener*>::iterator sit = m_keyListeners.begin(), eit = m_keyListeners.end(); sit != eit; ++sit)
			{
				intake::IKeyListener* currentKeyListener = *sit;
				if (currentKeyListener == keyListener)
				{
					it = sit;
					break;
				}
			}
			if (it != m_keyListeners.end())
			{
				m_keyListeners.erase(it);
			}
		}

		void InputEventHandler::injectKeyDown( intake::KeyCode key )
		{
			intake::KeyEvent* ke = new intake::KeyEvent(key);
			for (std::vector<intake::IKeyListener*>::iterator sit = m_keyListeners.begin(), eit = m_keyListeners.end(); sit != eit; ++sit)
			{
				intake::IKeyListener* currentKeyListener = *sit;
				if (currentKeyListener)
				{
					currentKeyListener->keyPressed(ke);
				}
			}
			delete ke;
		}

		void InputEventHandler::injectKeyUp( intake::KeyCode key )
		{
			intake::KeyEvent* ke = new intake::KeyEvent(key);
			for (std::vector<intake::IKeyListener*>::iterator sit = m_keyListeners.begin(), eit = m_keyListeners.end(); sit != eit; ++sit)
			{
				intake::IKeyListener* currentKeyListener = *sit;
				if (currentKeyListener)
				{
					currentKeyListener->keyReleased(ke);
				}
			}
			delete ke;
		}

	}

}